package views;


import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.wb.swt.ResourceManager;

import com.dj.tool.ReadModel;

import cn.com.zte.ngbc.base.util.JsonUtil;
import cn.com.zte.ngbc.frw.model.tran.FlowTransactionConf;

import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormLayout;


/**
 * This sample class demonstrates how to plug-in a new
 * workbench view. The view shows data obtained from the
 * model. The sample creates a dummy model on the fly,
 * but a real implementation would connect to the model
 * available either in this or another plug-in (e.g. the workspace).
 * The view is connected to the model using a content provider.
 * <p>
 * The view uses a label provider to define how model
 * objects should be presented in the view. Each
 * view can present the same model objects using
 * different labels and icons, if needed. Alternatively,
 * a single label provider can be shared between views
 * in order to ensure that objects of the same type are
 * presented in the same way everywhere.
 * <p>
 */

public class SampleView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "views.SampleView";

	private TableViewer viewer;
	private Action action1;
	private Action action2;
	private Action doubleClickAction;
	private Text text;
	private Text text_1;
	private Text text_2;
	private Text text_3;

	/*
	 * The content provider class is responsible for
	 * providing objects to the view. It can wrap
	 * existing objects in adapters or simply return
	 * objects as-is. These objects may be sensitive
	 * to the current input of the view, or ignore
	 * it and always show the same content 
	 * (like Task List, for example).
	 */
	 
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object parent) {
			return ReadModel.FileList();
			//return new String[] { "One", "Two", "Three" };
		}
	}

	class ViewContentProvider1 implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object parent) {
			return ReadModel.FileList();
			//return new String[] { "One", "Two", "Three" };
		}
	}
	
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().
					getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
		}
	}
	class NameSorter extends ViewerSorter {
	}

	/**
	 * The constructor.
	 */
	public SampleView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayout(new FormLayout());
		viewer = new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		Table table = viewer.getTable();
		FormData fd_table = new FormData();
		fd_table.left = new FormAttachment(0);
		fd_table.bottom = new FormAttachment(100, -10);
		table.setLayoutData(fd_table);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setSorter(new NameSorter());
		viewer.setInput(getViewSite());

		// Create the help context id for the viewer's control
//		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "menu.viewer");
//		getSite().setSelectionProvider(viewer);
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.right = new FormAttachment(lblNewLabel_1, 40);
		fd_lblNewLabel_1.top = new FormAttachment(0, 10);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("Port:");
		
		text = new Text(composite, SWT.BORDER);
		fd_lblNewLabel_1.left = new FormAttachment(text, 6);
		text.setText("127.0.0.1");
		FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(0, 380);
		fd_text.top = new FormAttachment(0);
		fd_text.left = new FormAttachment(0, 252);
		text.setLayoutData(fd_text);
		
		text_1 = new Text(composite, SWT.BORDER);
		text_1.setText("1101");
		FormData fd_text_1 = new FormData();
		fd_text_1.left = new FormAttachment(lblNewLabel_1);
		fd_text_1.right = new FormAttachment(lblNewLabel_1, 50, SWT.RIGHT);
		fd_text_1.top = new FormAttachment(0);
		text_1.setLayoutData(fd_text_1);
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		fd_table.right = new FormAttachment(lblNewLabel, -6);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.right = new FormAttachment(0, 252);
		fd_lblNewLabel.left = new FormAttachment(0, 218);
		fd_lblNewLabel.bottom = new FormAttachment(0, 30);
		fd_lblNewLabel.top = new FormAttachment(0, 10);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("ip:");
		
		text_2 = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		FormData fd_text_2 = new FormData();
		fd_text_2.right = new FormAttachment(table, 650);
		fd_text_2.left = new FormAttachment(table, 6);
		fd_text_2.top = new FormAttachment(lblNewLabel_1, 16);
		fd_text_2.bottom = new FormAttachment(100, -10);
		text_2.setLayoutData(fd_text_2);
		
		text_3 = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		FormData fd_text_3 = new FormData();
		fd_text_3.right = new FormAttachment(100, -10);
		fd_text_3.left = new FormAttachment(text_2, 5);
		fd_text_3.top = new FormAttachment(lblNewLabel_1, 16);
		fd_text_3.bottom = new FormAttachment(100, -10);
		text_3.setLayoutData(fd_text_3);
		
		//showMessage("发送报文。。。。"+text.getText()+":"+text_1.getText());
				
				Button btnNewButton = new Button(composite, SWT.NONE);
				fd_table.top = new FormAttachment(btnNewButton, 0, SWT.TOP);
				FormData fd_btnNewButton = new FormData();
				fd_btnNewButton.right = new FormAttachment(text, 320);
				fd_btnNewButton.left = new FormAttachment(text, 230, SWT.LEFT);
				fd_btnNewButton.top = new FormAttachment(0, 1);
				btnNewButton.setLayoutData(fd_btnNewButton);
				btnNewButton.addSelectionListener(new SelectionAdapter() {
					@Override
					public void widgetSelected(SelectionEvent e) {
					    if(text_2.getText().equals("")){
					    	text_3.setText("发送报文内容不能空!!");
					    	return;
					    } 
					   // text_3.setText("正在发送"+(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date())  );
						
						//showMessage("发送报文。。。。"+text.getText()+":"+text_1.getText());
						try {
							text_3.setText(	 ReadModel.send(text_2.getText(), text.getText(), text_1.getText() ) );
						} catch (IOException e1) {
							text_3.setText(e1.getMessage());
							e1.printStackTrace();
						}
					}
				});
				btnNewButton.setText("发送报文");
				fd_btnNewButton.bottom = new FormAttachment(lblNewLabel_1, 0, SWT.BOTTOM);

		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				SampleView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(action1);
		manager.add(new Separator());
		manager.add(action2);
	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(action1);
		manager.add(action2);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(action1);
		manager.add(action2);
	}

	private void makeActions() {
		action1 = new Action() {
			public void run() {
				showMessage("刷新交易，获取交易一共"+ReadModel.models.size()+"个，CTRL+F 快速搜索");
				viewer.setContentProvider(new ViewContentProvider());
			}
		};
		action1.setText("刷新交易");
		action1.setToolTipText("刷新交易");
		action1.setImageDescriptor(ResourceManager.getPluginImageDescriptor("CodegenPlugin", "icons/synced.gif"));
		
		action2 = new Action() {
			public void run() {
				showMessage("本报文视图用于交易调试使用，版本1.0.0，有问题及意见请邮件反馈1846292@qq.com");
			}
		};
		action2.setText("关于");
		action2.setToolTipText("关于");
		action2.setImageDescriptor(ResourceManager.getPluginImageDescriptor("CodegenPlugin", "icons/sample.gif"));
		doubleClickAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				FlowTransactionConf modelFile=(FlowTransactionConf)ReadModel.models.get(obj.toString());
				//showMessage("选中 "+obj.toString()+modelFile.getPackage()+modelFile.getDataInterface().toString() );
				//String jsonarray = JsonUtil.format(modelFile.getDataInterface().getInput().getElements()); 
				String text=ReadModel.context(modelFile);
				// JsonUtil.formatPkg(modelFile.getDataInterface().getInput().getElements());
				text_2.setText(text);
			}
		};
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	}
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"报文视图",
			message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
}
